//
//  TradeRecordView.m
//  BTCoin
//
//  Created by Robin on 29/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "TradeRecordView.h"
#import "TradeDealTableCell.h"
#import "EntrustInfoModel.h"
#import "NSDate+Util.h"
#import "MJRefresh.h"

@interface TradeRecordView ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) UILabel * emptyLabel;
@end

@implementation TradeRecordView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.emptyLabel = [[UILabel alloc] init];
        self.emptyLabel.textColor = color_333333;
        self.emptyLabel.text = @"暂无相关数据";
        self.emptyLabel.font = [UIFont systemFontOfSize:17.0];
        [self addSubview:self.emptyLabel];
        [self.emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        
        _tableView = [[UITableView alloc] init];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView registerNib:[UINib nibWithNibName:@"TradeDealTableCell" bundle:nil] forCellReuseIdentifier:@"TradeDealTableCell"];
        [self addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(self);
        }];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if ([UserInfo isLogin]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:REQUESTCNYDEALNOTIFICATION object:nil];
            } else {
                [[NSNotificationCenter defaultCenter] postNotificationName:REQUESTLOGINAPPNOTIFICATION object:nil];
                [_tableView.mj_header endRefreshing];
            }
        }];
    }
    
    return self;
}

- (void)updateView {
//    self.emptyLabel.hidden = self.model.tradeDealArray.count > 0;
//    self.tableView.hidden = self.model.tradeDealArray.count == 0;
    
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.model.tradeDealArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ChannelIdentifier = @"TradeDealTableCell";
    TradeDealTableCell *cell = [tableView dequeueReusableCellWithIdentifier:ChannelIdentifier forIndexPath:indexPath];
    
    [self configCell:cell indexPath:indexPath];
    
    return cell;
}

- (void)configCell:(TradeDealTableCell *)cell indexPath:(NSIndexPath *)indexPath {
    EntrustModel * model = self.model.tradeDealArray[indexPath.row];
    NSDate * date = [[NSDate alloc] initWithTimeIntervalSince1970:model.fcreateTime.time/1000.0];
    cell.dateLabel.text = [date stringWithDateFormate:@"yyyy-MM-dd HH:mm:ss"];
    
    cell.typeLabel.text = model.fentrustType==0?@"买入":(model.fentrustType==1?@"卖出":@"");
    cell.countLabel.text = [NSString stringWithFormat:@"%ld",model.fcount];
    cell.prizeLabel.text = [NSString stringWithFormat:@"%.4f",model.fprize];
    
    cell.amountLabel.text = [NSString stringWithFormat:@"%ld",model.famount];
    cell.dealAmountLabel.text = [NSString stringWithFormat:@"%ld",model.fcount-model.fleftCount];
    cell.successAmountLabel.text = [NSString stringWithFormat:@"%ld",model.fsuccessAmount];
    
    cell.feesLabel.text = [NSString stringWithFormat:@"%.4f",model.ffees];
    if (model.fcount == model.fleftCount) {
        cell.feesLabel.text = @"0.0000";
    } else {
        float averagePrice = (model.fsuccessAmount / (model.fcount - model.fleftCount));
        cell.feesLabel.text = [NSString stringWithFormat:@"%.4f",averagePrice];
    }
    
    if (model.fstatus == 3) {
        cell.statusLabel.text = @"成交";
    } else if (model.fstatus == 4) {
        cell.statusLabel.text = @"用户撤销";
    }
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 136.0;
}


@end
